package mlProject;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import mlProject.DocModel.Feature;

public class DocModelReader {
	public static ArrayList<DocModel> readFromFile(String fileName) {
		ArrayList<DocModel> docModels = new ArrayList<>();

		String path = "src/main/resources/"+fileName;
		FileReader fr;
		try {
			fr = new FileReader(path);
			BufferedReader br = new BufferedReader(fr);
			String row;
			while ((row = br.readLine()) != null) {
				DocModel docModel = new DocModel();
				String[] words = row.split(" ");
				docModel.id = words[0];
				double max=-1.0;
				for (int i = 1; i < words.length - 1; i++) {
					String[] kv = words[i].split(":");
					Feature f = new Feature(kv[0], Double.valueOf(kv[1]));
					if(Double.valueOf(kv[1])>max) max=Double.valueOf(kv[1]);
					docModel.addFeature(f);
					docModel.sum+=Double.valueOf(kv[1]);
				}
				String label = (words[words.length - 1].split(":"))[1].trim();
				docModel.label = label;
				docModel.max=max;
				docModel.toTFIDF();
				docModels.add(docModel);
			}
			br.close();
			fr.close();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return docModels;
	}

	public static void main(String args[]) {
		ArrayList<DocModel> models = readFromFile("review.train");
		System.out.println(models.size());
//		for(int i = 0 ; i < models.size() ; i++){
//			System.out.println(models.get(i));
//		}
		System.out.println(models);
	}
}
